package tech.wangpai.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import tech.wangpai.domain.User;

import java.math.BigDecimal;
import java.time.LocalDate;

@Service
public class TestHttpService {
    @Autowired
    private RestTemplate restTemplate;

    @Retryable(value = RestClientException.class, maxAttempts = 3,
            backoff = @Backoff(delay = 5000l,multiplier = 1))
    public String queryUser(String username){
        User user = new User();
        user.setUsername("andy");
//        String url = "http://localhost/users";
        String url = "http://localhost/xxx";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, user);

        if(!responseEntity.getStatusCode().is2xxSuccessful()){
            System.out.println("请求失败...");
            throw new RestClientException(responseEntity.getBody());
        }

        String body = responseEntity.getBody();
        HttpStatus statusCode = responseEntity.getStatusCode();
        int statusCodeValue = responseEntity.getStatusCodeValue();
        HttpHeaders headers = responseEntity.getHeaders();
        System.out.println("body：" + body);
        System.out.println("statusCode：" + statusCode);
        System.out.println("statusCodeValue：" + statusCodeValue);
        System.out.println("headers：" + headers);
        return "test";
    }

    public void getUserById(){
        String url = "http://localhost/user/1";
        ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class);
        User body = responseEntity.getBody();
        HttpStatus statusCode = responseEntity.getStatusCode();
        int statusCodeValue = responseEntity.getStatusCodeValue();
        HttpHeaders headers = responseEntity.getHeaders();

        System.out.println("body：" + body);
        System.out.println("statusCode：" + statusCode);
        System.out.println("statusCodeValue：" + statusCodeValue);
        System.out.println("headers：" + headers);
    }

    public void addUser(){
        HttpHeaders reqHeader = new HttpHeaders();
        reqHeader.setContentType(MediaType.APPLICATION_JSON_UTF8);

        User user = new User();
        user.setUserId("5");
        user.setAge(20);
        user.setUsername("test");
        user.setScore(new BigDecimal("90"));
        user.setPassword("1234565");
        user.setBirthday(LocalDate.now());

        HttpEntity<User> request = new HttpEntity<>(user, reqHeader);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost/user", request, String.class);

        String body = responseEntity.getBody();
        HttpStatus statusCode = responseEntity.getStatusCode();
        int statusCodeValue = responseEntity.getStatusCodeValue();
        HttpHeaders headers = responseEntity.getHeaders();

        System.out.println("body：" + body);
        System.out.println("statusCode：" + statusCode);
        System.out.println("statusCodeValue：" + statusCodeValue);
        System.out.println("headers：" + headers);
    }

    public void deleteUserById(){
        String url = "http://localhost/user/4";
        restTemplate.delete(url);
    }

    public void updateUserById(){
        String url = "http://localhost/user/4";
        User user = new User();
        user.setUserId("1");
        user.setPassword("999999");
        restTemplate.put(url,user);
    }

}
